/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package da;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author HungVM_C00359
 */
public abstract class BaseConnection {
    public static String host="";
    public static String name="";
    public static String passWord="";
    public static String dataBase="";
    public static int port;

    protected Connection connect = null;
    protected Statement stm = null;
    protected CallableStatement cstm =null;
    protected ResultSet rs = null;

    public BaseConnection() {
    }


    public abstract Connection getConnection() throws Exception;
    public abstract void testDriver() throws Exception;

    public Statement getStatement() throws Exception{
        if(this.stm == null){
            this.stm = this.getConnection().createStatement();
        }
        return this.stm;
    }


    public ResultSet excuteQuery(String Query) throws Exception{
        try {
            this.rs = getStatement().executeQuery(Query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this.rs;
    }

    public int excuteUpdate(String Query) throws Exception{
        int res = Integer.MIN_VALUE;
        try {
            res = getStatement().executeUpdate(Query);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            this.myClose();
        }
        return res;
    }

    public void myClose() throws SQLException{
        //if ResultSet is not closed then close:
        if(this.rs !=null){
            if(!this.rs.isClosed()){
                this.rs.close();
            }
        }

        if(this.stm!=null){
            if(!this.stm.isClosed()){
                this.rs.close();
            }

        }

        if(this.connect !=null){
            if(!this.connect.isClosed()){
                this.connect.close();
            }
            this.connect = null;
        }
        //System.out.println("Close connect data base");
    }



}
